package com.mingshashan.building.easy.core.exception;

import com.mingshashan.building.easy.core.common.SpringApplicationContextHolder;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ResourceBundleMessageSource;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.concurrent.ConcurrentHashMap;

public class EasyErrorCodeMessages {
    private static Logger logger = LoggerFactory.getLogger(EasyErrorCodeMessages.class);
    private static ConcurrentHashMap<String, ResourceBundleMessageSource> messageSources = new ConcurrentHashMap();

    private static ResourceBundleMessageSource getMessageSource() {
        final String key = "i18n.i18n.exception.messages";

        ResourceBundleMessageSource messageSource = (ResourceBundleMessageSource) messageSources.get(key);

        if (messageSource != null) {
            return messageSource;
        }
        messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename(key);
        messageSource.setUseCodeAsDefaultMessage(true);
        messageSource.setAlwaysUseMessageFormat(true);
        messageSources.putIfAbsent(key, messageSource);
        return (ResourceBundleMessageSource) messageSources.get(key);
    }

    private static String getMessage(Locale locale, ErrorCoded errorCoded, Object... params) {
//        return getLocalErrorMessage(errorCoded, locale, params);
        String message = errorCoded.getErrorName();
        if (errorCoded == null) {
            throw new NullPointerException("ErrorCode is null");
        }
        if (StringUtils.isBlank(errorCoded.getErrorName())) {
            throw new IllegalArgumentException("ErrorCode.code is blank");
        }
        if (locale == null) {
            locale = Locale.getDefault();
        }
        try {
            String resourceKey = "i18n.exception.easy_messages";
            ResourceBundle bundle = ResourceBundle.getBundle(resourceKey, locale);
            message = bundle.getString(errorCoded.getErrorName());
        } catch (Throwable e) {
            /* 不记入日志, 可以减少依赖 */
            e.printStackTrace();
        }
        try {
            return new MessageFormat(message).format(params);
        } catch (Throwable e) {
            return message;
        }
    }

    public static String getLocalErrorMessage(ErrorCoded errorCoded, Locale locale, Object... args) {
        ApplicationContext applicationContext = SpringApplicationContextHolder.getApplicationContext();
        String pattern = applicationContext.getMessage(errorCoded.getErrorName(), args, locale);

        if (StringUtils.isBlank(pattern)) {
            return errorCoded.getErrorName();
        }

        return MessageFormat.format(pattern, args);

    }

    public static String getMessage(ErrorCoded errorCoded) {
        return getMessage(Locale.getDefault(), errorCoded, (Object[]) null);
    }

    public static String getMessage(ErrorCoded errorCoded, Object... params) {
        return getMessage(Locale.getDefault(), errorCoded, params);
    }

    private static String getMessage1(Locale locale, ErrorCoded errorCoded, Object... params) {
        if (errorCoded == null) {
            throw new NullPointerException("ErrorCode is null");
        }
        if (StringUtils.isBlank(errorCoded.getErrorName())) {
            throw new IllegalArgumentException("ErrorCode.code is blank");
        }
        if (locale == null) {
            locale = Locale.getDefault();
        }
        try {
            return getMessageSource().getMessage(errorCoded.toString(), params, locale);
        } catch (Throwable e) {
            logger.error(e.toString(), e);
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}